package com.huiyujinrong.qiangdanbao.activity.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.chrisbanes.photoview.PhotoView;
import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.utils.DensityUtils;
import com.huiyujinrong.qiangdanbao.utils.ScreenShot;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.utils.Log;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 展业神器编辑页面
 * Created by ZQXQ-Developer on 2017/6/30.
 */

public class ExhibitionEditActivity extends AppCompatActivity {

    @BindView(R.id.toolbar_Back)
    RelativeLayout back;
    @BindView(R.id.toolbar_Title)
    TextView tvTitle;

    @BindView(R.id.edEName)
    EditText etName;
    @BindView(R.id.etEJob)
    EditText etJob;
    @BindView(R.id.etEPhone)
    EditText etPhone;
    @BindView(R.id.tvExName)
    TextView tvName;

    @BindView(R.id.tvPhone)
    TextView tvPhone;
    @BindView(R.id.tvEJob)
    TextView tvJob;

    @BindView(R.id.tvRlayout)
    RelativeLayout relativeLayout;

    @BindView(R.id.image10)
    ImageView saveImage;
    public Bitmap bitmap;
    Context context;
    @BindView(R.id.rlBrowse)
    RelativeLayout reBrowse;//浏览

    @BindView(R.id.photoView)
    PhotoView photoView;

    @BindView(R.id.llView)
    LinearLayout llView;
    public Activity activity;
    public int x, y, width, height;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        activity = this;
        setContentView(R.layout.ui_exhibition_edit);
        ButterKnife.bind(this);
        tvTitle.setText("编辑内容");
        back.setVisibility(View.VISIBLE);
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() < 5) {
                    tvName.setText(charSequence.toString());
                } else {
                    Toast.makeText(ExhibitionEditActivity.this, "名字最长为4个字", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etJob.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                tvJob.setText(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                tvPhone.setText(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.toolbar_Back, R.id.tvShare,
            R.id.tvSave, R.id.tvBrowse, R.id.browse})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_Back:
                finish();
                break;
            case R.id.tvShare:
                if (isNoNull()) {
                    bitmap = ScreenShot.takeScreenShotClip(activity, x, y, width, height);
                    String ok = ScreenShot.savePic(context, bitmap);
                    toShare(bitmap);
                }
                break;
            case R.id.tvSave:
                // 裁取屏幕的指定大小
                if (!isNoNull()) {
                    return;
                }
                bitmap = ScreenShot.takeScreenShotClip(activity, x, y, width, height);
                String ok = ScreenShot.savePic(context, bitmap);
                if (!TextUtils.isEmpty(ok)) {
                    saveImage.setImageBitmap(bitmap);
                    Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tvBrowse:
                if (!isNoNull()) {
                    return;
                }
                bitmap = ScreenShot.takeScreenShotClip(activity, x, y, width, height);
                photoView.setImageBitmap(bitmap);
                reBrowse.setVisibility(View.VISIBLE);
                llView.setVisibility(View.GONE);
                break;
            case R.id.browse:
                reBrowse.setVisibility(View.GONE);
                llView.setVisibility(View.VISIBLE);
                break;
        }
    }

    public boolean isNoNull() {
        if (TextUtils.isEmpty(tvName.getText().toString())) {
            showToast("请填写姓名");
            return false;
        }
        if (TextUtils.isEmpty(tvJob.getText().toString())) {
            showToast("请填写职位");
            return false;
        }
        if (TextUtils.isEmpty(tvPhone.getText().toString())) {
            showToast("请填写电话");
            return false;
        }
        return true;
    }

    private void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    private void toShare(Bitmap bitmap) {
        new ShareAction(this)
                .withText("hello")
                .withMedia(new UMImage(this, bitmap))
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,
                        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setCallback(umShareListener).open();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            Toast.makeText(ExhibitionEditActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ExhibitionEditActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ExhibitionEditActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        setOptions(relativeLayout);//firstLayout
    }

    /**
     * 获取名片布局的宽高
     *
     * @param pLayout
     */
    private void setOptions(RelativeLayout pLayout) {
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID  
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值  
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        //
        int bottom = pLayout.getBottom() + DensityUtils.dip2px(this, 45) + statusBarHeight1;
        int top = pLayout.getTop() + DensityUtils.dip2px(this, 45) + statusBarHeight1;
        int left = pLayout.getLeft();
        int right = pLayout.getRight();
        float xx = pLayout.getX();
        float yy = pLayout.getY();
        //pLayout.getParent();
        Log.e("getLocationInWindow", "bottom: " + DensityUtils.px2dip(this, bottom) + "    top:" + DensityUtils.px2dip(this, top));
        Log.e("getLocationInWindow", "bottom: " + bottom + "   top:" + top + "    left:" + left + "     right:" + right + "     xx:" + xx + "    yy:" + yy);
        x = left;
        y = top;
        width = right - left;
        height = bottom - top;
        Log.e("getLocationInWindow", "  x:" + x + "   y:" + y + "    width:" + width + "   height:" + height);
    }

}
